﻿using Consul;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace UserServiceB.Controllers
{
    [Route("user/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly ConsulClient consulClient;

        public HomeController(ConsulClient consulClient)
        {
            this.consulClient = consulClient;
        }

        [HttpGet]
        public IActionResult Index()
        {
            return Ok("用户服务B");
        }

        /// <summary>
        /// 获取服务列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetService()
        {
            var list = await consulClient.Agent.Services();
            return Ok(list.Response);
        }

        /// <summary>
        /// 注销服务
        /// </summary>
        /// <param name="ServiceId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DeRegisterService(string ServiceId)
        {
            var result = await consulClient.Agent.ServiceDeregister(ServiceId);
            return Ok(result);
        }

        /// <summary>
        /// 健康检查
        /// </summary>
        /// <returns></returns>
        [HttpGet("/healthcheck")]
        public IActionResult HealthCheck()
        {
            return Ok("Success");
        }
    }
}
